package gui;

import effect.Effect;
import gui.GUIConstants.PopupSize;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Oscar
 * A class for writing out the effects when you mouseover them. This popup
 * panel is showd in the bottom left corner of the CenterPanel. To determine
 * the size of this panel the enum InfoPopupSize.
 */
public class EffectPopupInfo {
	
	private static EffectPopupInfo me;
	
	private PopupSize size;
	private String name;
	private List<String> description = new ArrayList<String>();
	private int goldCost;
	private int manaCost;
	
	public static EffectPopupInfo getInstance(){
		if(me == null)
			me = new EffectPopupInfo();
		return me;
	}
	
	/**
	 * The constructor for EffectPopupInfo needs three variables, the size of the popup
	 * panel, the name of the effect as well as the description of the effect.
	 * @param ps
	 * @param n
	 * @param d
	 */
	public EffectPopupInfo(){
		setName(null);
		setDescription(" ");
		setSize(PopupSize.large);
	}
	
	
	public PopupSize getSize() {
		return size;
	}

	public void setSize(PopupSize size) {
		this.size = size;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<String> getDescription() {
		return description;
	}
	
	

	public void setDescription(String d) {
		description = fixDescription(d);
	}
	
	public void setProperties(Effect e){
		name = e.getName();
		description = fixDescription(e.getDescription());
		goldCost = e.getGoldCost();
		manaCost = e.getManaCost();
	}
	
	public void setProperties(String n, String d){
		name = n;
		description = fixDescription(d);
	}
	
	public void setProperties(String n, String d, PopupSize ps){
		name = n;
		description = fixDescription(d);
		size = ps;
	}

	/**
	 * This method calls different methods with the goal of painting the 
	 * popup with the correct info and size. Depending on the InfoPopupSize 
	 * parameter the size of this window is determined. The parameter panelHeight
	 * is used to determine where in the CenterPanel the popup should be painted.
	 * @param g
	 * @param panelHeight
	 */
	public void paint(Graphics g, int panelHeight){
		int amountOfVerticalBorders = 0;
		switch(size){
		//Height: 100px
		case small:
			amountOfVerticalBorders = 2;
			break;
		//Height: 150px
		case medium:
			amountOfVerticalBorders = 3;
			break;
			//Height: 200px
		case large:
			amountOfVerticalBorders = 4;
			break;
		}
		
		paintBackground(g, panelHeight, amountOfVerticalBorders);
		paintBorder(g, panelHeight, amountOfVerticalBorders);
		paintText(g, panelHeight, amountOfVerticalBorders, name, description);
		paintCost(g, panelHeight, amountOfVerticalBorders);
	}
	
	private void paintBackground(Graphics g, int panelHeight, int heightMultiplier){
		for(int i = 0; i<100; i++)
			for(int j = 0; j<((50*heightMultiplier)/2); j++)
				ImageHandler.getInstance().drawImage(5+i*2,panelHeight-(25+(50*heightMultiplier))+j*2, 2, 2, "infoPopupBackground.png", g);
	}
	
	private void paintBorder(Graphics g, int panelHeight, int heightMultiplier){
		for(int i = 1; i<=(heightMultiplier); i++){
			ImageHandler.getInstance().drawImage(4,panelHeight-25-i*50, 1, 50, "infoPopupBorderVertical.png", g);
			ImageHandler.getInstance().drawImage(204,panelHeight-25-i*50, 1, 50, "infoPopupBorderVertical.png", g);
		}
		for(int i = 0; i<4; i++){
			ImageHandler.getInstance().drawImage(4+i*50,panelHeight-(25+(50*heightMultiplier)), 50, 1, "infoPopupBorderHorizontal.png", g);
			ImageHandler.getInstance().drawImage(4+i*50,panelHeight-25, 50, 1, "infoPopupBorderHorizontal.png", g);
		}
	}
	
	private void paintText(Graphics g, int panelHeight, int heightMultiplier, String name, List<String> description){
		g.setColor(new Color(226, 225, 223));
		g.setFont(new Font("Dialog", Font.BOLD, 13));
		g.drawString(name, 25, panelHeight-((heightMultiplier*50)));
		g.setFont(new Font("Dialog", Font.BOLD, 11));
		for(int i = 0; i < description.size(); i++)
			g.drawString(description.get(i), 15, panelHeight-((heightMultiplier*50))+20+i*20);
	}
	
	private void paintCost(Graphics g, int panelHeight, int heightMultiplier){
		g.setColor(new Color(226, 225, 223));
		g.setFont(new Font("Dialog", Font.BOLD, 15));
		ImageHandler.getInstance().drawImage(15, panelHeight-((heightMultiplier*50))+145, 30, 21, "gold.png", g);
		g.drawString(""+goldCost, 50, panelHeight-((heightMultiplier*50))+161);
		ImageHandler.getInstance().drawImage(85, panelHeight-((heightMultiplier*50))+142, 25, 25, "mana.png", g);
		g.drawString(""+manaCost, 115, panelHeight-((heightMultiplier*50))+161);

	}
	
	/**
	 * Method used for converting a String to a list of strings. The reason for doing
	 * this is because we want lines with the correct length in the popup. Otherwise we would
	 * get lines of text that are longer then the actual popup panel.
	 * @param s
	 * @return List<String>
	 */
	private List<String> fixDescription(String s){
		List<String> descriptionList = new ArrayList<String>();
		
		int lineLength = 20;
		int lineStart = 0;
		
		for(int i = 0; i < s.length(); i++){
			if(i >= lineLength){
				if(s.charAt(i) == ' ' || 
						s.charAt(i) == '.' ||
						s.charAt(i) == '?' ||
						s.charAt(i) == '!' ||
						i == (s.length()-1)){
					descriptionList.add(s.substring(lineStart, i));
					lineLength = i+25;
					lineStart = i+1;
				}
			}else if(i == (s.length()-1)){
				descriptionList.add(s.substring(lineStart, i));
			}
		}
		return descriptionList;
	}
}